package com.eascs.spider.ant.worker.spider;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.eascs.spider.ant.common.proj.Platform;
import com.eascs.spider.ant.common.proj.Task;

/**
 * 惠农网采购大厅数据爬取spider
 * @author hx
 *
 */
public class CnhnbPurchaseSpider extends AnotherSpider{

	private Map<String, Object> data = new HashMap<String, Object>();
	
	public CnhnbPurchaseSpider(Task task) {
		super(task);
	}

	@Override
	public void run() {
		String html = downloader.setUrl(task.getUrl())
								.setParam(task.getParamMap())
								.setHeader(task.getHeaderMap())
								.setHttpMethod(Task.HTTP_GET)
								.download().getHtml();
		Pattern pattern = Pattern.compile("http://www.cnhnb.com/caigou/.*?");
		if(pattern.matcher(task.getUrl()).find()) {
			getInfo(html);
			logger.info("单条数据，url:{}",task.getUrl());
			mongoSession.setData(data, Platform.CNHNB_PURCHASE);
		} else {
			extractPage(html);
		}
	}
	
	public void extractPage(String html) {
		if(getUrl(html)) {
			Task t = new Task();
			t.setMethod(Task.HTTP_GET)
			.setHomepage(Task.HOMEPAGE_NO)
			.setHeaderMap(task.getHeaderMap());
			Pattern pattern = Pattern.compile("http://www.cnhnb.com/purchase/0-0-0-0-0-(\\d+)");
			Matcher matcher = pattern.matcher(task.getUrl());
			if(matcher.find()) {
				t.setUrl("http://www.cnhnb.com/purchase/0-0-0-0-0-"+(Integer.valueOf(matcher.group(1))+1));
			} else {
				t.setUrl("http://www.cnhnb.com/purchase/0-0-0-0-0-2");
			}
			//把t加到队列里去
			manager.addSendQueue(t);
		}
	}
	
	public boolean getUrl(String html) {
		boolean flag = true;
		Document document = Jsoup.parse(html);
		Elements elements = document.select("li.text-list");
		Iterator<Element> iterator = elements.iterator();
		while(iterator.hasNext()) {
			Element element = iterator.next();
			String time = element.select("span.l1").text();
			if(time.contains("天前")) {
				flag = false;
			} else {
				String url = element.select("a").attr("href");
				//将是当天发布的消息的链接加入到队列
				Task t = new Task();
				t.setUrl(url)
				.setMethod(Task.HTTP_GET)
				.setHomepage(Task.HOMEPAGE_NO)
				.setHeaderMap(task.getHeaderMap());
				manager.addSendQueue(t);
			}
		}
		if(flag) {
			logger.info("该页面数据均为当日数据，url：{}",task.getUrl());
		} else {
			logger.info("该页面存在过期数据，分页爬取结束，url：{}",task.getUrl());
		}
		return flag;
	}
	
	public void getInfo(String html) {
		try {
			Document document = Jsoup.parse(html);
			SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
			Elements elements = document.select("ul.details-content li");
			String title = elements.get(0).text();   //标题
			for(int i=1;i<elements.size();i++) {
				String text = elements.get(i).text();
				String[] split = text.split("：");
				data.put(split[0], split[1]);
			}
			Date time = s.parse(document.select("li.details-times").text().replaceAll("发布时间：", ""));  //发布时间
			String visitCount = document.select("span.visCount").text().equals("") ? "0" : document.select("span.visCount").text();      //浏览次数
			String name = document.select("li.contact-name").text();          //采购商姓名
			String address = document.select("li.contact-location").text();   //采购商地址
			Date overTime = s.parse(document.select("p.icon-time").text().replaceAll("截止日期：", ""));  //截止日期
			String remark = document.select("div.details-wrapper").first().select("li").text();   //补充说明
			data.put("发布时间", time);
			data.put("浏览次数", visitCount);
			data.put("标题", title);
			data.put("发布人", name);
			data.put("采购商地址", address);
			data.put("截止日期", overTime);
			data.put("url", task.getUrl());
			data.put("crawler_time", new Date());
			if(remark.equals("")) {
				data.put("补充说明", "无");
			} else {
				data.put("补充说明", remark);
			}
		} catch (ParseException e) {
			logger.error("字符串转date出错！", e);
		} catch (IndexOutOfBoundsException e) {
			logger.error("由于网络原因，无法获取全部页面内容！");
			reAddTask();
		}
	}
	
	public void reAddTask() {
		task.setHomepage(Task.HOMEPAGE_NO);
		manager.addSendQueue(task);
		logger.debug("链接重新加入队列，url：{}", task.getUrl());
	}

}
